﻿/*****************************************************
 * 本类库的核心系 JNTemplate
 * (C) JNTemplate 2011
 * 作者：翅膀的初衷 QQ:4585839
 * 网址：http://www.JiNianNet.com
 *****************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace JinianNet.JNTemplate.Process.Common
{
    public class FileHelper
    {
        public static bool IsVirtualPath(string path) 
        {
            return (string.IsNullOrEmpty(path) || path.Length < 2 || !Char.IsLetter(path[0]) || path[1] != ':');
        }

        public static string ToRealPath(string virtualPath)
        {
            return ToRealPath(virtualPath, HttpContext.Current.Server.MapPath("~/"));
        }

        public static string ToRealPath(string virtualPath,string basePath)
        {
            if (string.IsNullOrEmpty(virtualPath) || virtualPath.Trim().Length == 0)
                throw new ArgumentNullException("Path");
            if (string.IsNullOrEmpty(basePath))
            {
                basePath = HttpContext.Current.Server.MapPath("~");
            }
            if (IsVirtualPath(virtualPath))
            {
                if (virtualPath[0] == '/')
                    return HttpContext.Current.Server.MapPath(string.Concat("~", virtualPath));
                else if (virtualPath[0] == '~')
                    return HttpContext.Current.Server.MapPath(virtualPath);
                else
                    return string.Join("\\", new string[] { basePath, virtualPath.Replace("/","\\") }).Replace("\\\\","\\").Trim(); // basePath[basePath.Length-1]=='/' ? string.Concat(basePath, virtualPath) : string.Concat(basePath, "/", virtualPath);
            }
            else
                return virtualPath;
        }

        public static string Load(string path)
        {
            if (string.IsNullOrEmpty(path))
                return string.Empty;
            return Load(path, Encoding.Default);
        }
        public static string Load(string path, Encoding charset)
        {
            try
            {
                //if (System.Web.HttpContext.Current != null)
                //{
                //    if (path.StartsWith("/"))
                //        path = System.Web.HttpContext.Current.Server.MapPath("~" + path);
                //    else if (path.StartsWith("~/"))
                //        path = System.Web.HttpContext.Current.Server.MapPath(path);
                //    else
                //    {
                //        if (!path.Remove(0, 1).StartsWith(":"))
                //        {
                //            string url = System.Web.HttpContext.Current.Request.PhysicalPath;
                //            if (!string.IsNullOrEmpty(url))
                //            {
                //                int index = System.Web.HttpContext.Current.Request.PhysicalPath.LastIndexOf("\\");
                //                string _path = url.Substring(0, index) + "\\" + path.Replace("/", "\\");
                //                if (!System.IO.File.Exists(_path))
                //                    path = System.Web.HttpContext.Current.Server.MapPath("~/") + path.Replace("/", "\\");
                //                else
                //                    path = _path;
                //            }
                //            else
                //            {
                //                url = System.Web.HttpContext.Current.Request.PhysicalApplicationPath;
                //                if (!string.IsNullOrEmpty(url))
                //                {
                //                    path = url + path.Replace("/", "\\");
                //                }
                //                else
                //                {
                //                    path = System.Web.HttpContext.Current.Server.MapPath("~/") + path.Replace("/", "\\");
                //                }
                //            }
                //        }
                //    }
                //}
                //else
                //{
                //    if (path.StartsWith("/"))
                //        path = System.Web.HttpRuntime.AppDomainAppPath + path.Remove(0, 1).Replace("/", "\\");
                //    else if (path.StartsWith("~/"))
                //        path = System.Web.HttpRuntime.AppDomainAppPath + path.Remove(0, 2).Replace("/", "\\");
                //    else
                //    {
                //        if (!path.Remove(0, 1).StartsWith(":"))
                //        {
                //            path = System.Web.HttpRuntime.AppDomainAppPath + path.Replace("/", "\\");
                //        }
                //    }
                //}
                path = ToRealPath(path.Trim());

                if (System.IO.File.Exists(path))
                    return System.IO.File.ReadAllText(path, charset);
                else
                    return "路径[ " + path + " ]未找到";
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }
        public static void Save(string text, string path, Encoding charset)
        {
            if (string.IsNullOrEmpty(path))
                return;

            System.IO.FileInfo info = new System.IO.FileInfo(path);
            if (!info.Directory.Exists)
                info.Directory.Create();
            if (info.Exists)
                info.Delete();

            using (System.IO.StreamWriter sw = new System.IO.StreamWriter(path, false, charset))
            {
                try
                {
                    sw.Write(text);
                }
                catch
                {
                    throw;
                }
                finally
                {
                    sw.Close();
                    sw.Dispose();
                }
            }
        }
        public static void Save(string text, string path)
        {
            Save(text, path, Encoding.Default);
        }
    }
}
